// import 'dart:convert';

import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:study1/model/v1user.dart';
import 'package:dio/dio.dart';

class UserList extends StatefulWidget {
  const UserList({Key? key}) : super(key: key);

  @override
  _UserListState createState() => _UserListState();
}

class _UserListState extends State<UserList> {
  late List<V1user> userlist = [];

  @override
  void initState() {
    getuserlist();
    super.initState();
  }

  void getuserlist() async {
    Dio dio = Dio();
    String url = "http://221.193.206.189:60000/admin/getv1userlist";
    Map<String, dynamic> map = Map();
    map["test"] = "test";
    // map["password"] = password;
    Response response = await dio.post(url, data: map);
    if (response.statusCode == 200) {
      print("响应数据:${response.data}");

      /// 更新UI
      setState(() {
        var listDynamic = jsonDecode(response.data);

        List<Map<String, dynamic>> listMap =
            new List<Map<String, dynamic>>.from(listDynamic);
        List<V1user> M = [];
        listMap.forEach((m) => M.add(V1user.fromJson(m)));

        /// 解析数据

        userlist = M;
        print("My Data:${M[0].nickname}");
      });
    } else {
      print("失败${response.statusCode}");
    }
  }

  Widget userlistwidget() {
    return ListView.separated(
        separatorBuilder: (BuildContext context, int index) {
          return Divider(
            color: Colors.black12,
          );
        },
        itemCount: userlist == null ? 0 : userlist.length,
        itemBuilder: (content, index) {
          return _listItem(userlist[index]);
        });
  }

  Widget _listItem(V1user user) {
    return Container(
      height: ScreenUtil().setHeight(50),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            decoration: new BoxDecoration(
              // color: Colors.grey,
            ),
            child: Text(
              user.danweiminghcneg ?? "单位",
            ),
            width: ScreenUtil().setWidth(100),
          ),
          SizedBox(
            width: ScreenUtil().setWidth(20),
          ),
          Container(
            decoration: const BoxDecoration(
              // color: Colors.grey,
            ),
            child: Text(
              user.nickname,
              textAlign: TextAlign.left,
            ),
            width: ScreenUtil().setWidth(200),
          ),
          SizedBox(
            width: ScreenUtil().setWidth(10),
          ),
          Container(
            decoration: const BoxDecoration(
              // color: Colors.grey,
            ),
            child: Text(
              user.username!,
              textAlign: TextAlign.left,
            ),
            width: ScreenUtil().setWidth(200),
          ),

          SizedBox(
            width: ScreenUtil().setWidth(180),
          ),
          const Icon(
            Icons.arrow_forward_ios,
            size: 15,
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "用户列表",
          style: TextStyle(color: Colors.white),
        ),
        leading: const Icon(
          Icons.menu,
          color: Colors.white,
        ),
        backgroundColor: Colors.green[500],
      ),
      body: userlistwidget(),
    );
  }
}
